#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <sys/vfs.h>
#include "oledfont.h"

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

int fd;                    // WiringPi I2C Section Num
char DisplayBuffer[8][21]; // Font Size is 8x6 -> 8Line x 21 Chars
int hour;
void InitOled()
{
    wiringPiSetup();
    fd = wiringPiI2CSetup(0x3C);          // Init I2C, 0x3C is the address of SSD1306
    wiringPiI2CWriteReg8(fd, 0x00, 0xA1); // Change 0xA1 to 0xA0 to reverse display
    wiringPiI2CWriteReg8(fd, 0x00, 0xC8); // Change 0xC8 to 0xC0 to reverse line direction
    wiringPiI2CWriteReg8(fd, 0x00, 0x8D);
    wiringPiI2CWriteReg8(fd, 0x00, 0x14);
    wiringPiI2CWriteReg8(fd, 0x00, 0xA6); // Change 0xA6 to 0xA7 to reverse color
    wiringPiI2CWriteReg8(fd, 0x00, 0x21);
    wiringPiI2CWriteReg8(fd, 0x00, 0x00);
    wiringPiI2CWriteReg8(fd, 0x00, 0x7F);
    wiringPiI2CWriteReg8(fd, 0x00, 0xAF);
}
void ClearOled()
{
    int i, j;
    for (i = 0; i < 8; i++)
    {
        wiringPiI2CWriteReg8(fd, 0x00, 0xb0 + i);
        for (j = 0; j < 128; j++)
            wiringPiI2CWriteReg8(fd, 0x40, 0x00); // Clear Display
    }
}
void ClearBuffer(int line) // line=-1 for all line
{
    int i, j;
    if (line == -1)
    {
        for (i = 0; i < 8; i++)
            for (j = 0; j < 21; j++)
                DisplayBuffer[i][j] = 0x20; // Use Space to Clear Buffer
    }
    else
    {
        i = line;
        for (j = 0; j < 21; j++)
            DisplayBuffer[i][j] = 0x20;
    }
}
void UpdateBuffer()
{
    int i, j, k;
    for (i = 0; i < 8; i++)
    {
        wiringPiI2CWriteReg8(fd, 0x00, 0xb0 + i);
        for (j = 0; j < 21; j++)
            for (k = 0; k < 6; k++)
                wiringPiI2CWriteReg8(fd, 0x40, F6x8[DisplayBuffer[i][j] - 0x20][k]);
        wiringPiI2CWriteReg8(fd, 0x40, 0x00); // Fit in 128 Byte
        wiringPiI2CWriteReg8(fd, 0x40, 0x00);
    }
}
void MyCharCopy(char *dst, char *src, int maxlen)
{
    int i;
    for (i = 0; i < maxlen; i++)
    {
        if (*src == 0)
            break;
        if (*src == '\n' || *src == '\r')
            *dst = 0x32;
        else
            *dst = *src;
        dst++;
        src++;
    }
}
void GetSysTime()
{
    char tmpstr[30];
    struct tm *p;
    time_t timep;
    ClearBuffer(0);
    time(&timep);
    p = localtime(&timep);
    //	sprintf(tmpstr,"%d/%02d/%02d %02d:%02d",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday,p->tm_hour,p->tm_min);
    //	MyCharCopy(DisplayBuffer[0],tmpstr,21);
    hour = p->tm_hour;
}
void GetIP(char *name, int line)
{
    char tmpstr[30];
    int rc = 0;
    struct sockaddr_in *addr = NULL;
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(struct ifreq));

    /* 0. create a socket */
    int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (fd == -1)
        return;

    /* 1. set type of address to retrieve : IPv4 */
    ifr.ifr_addr.sa_family = AF_INET;

    /* 2. copy interface name to ifreq structure */
    strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);

    /* 3. get the IP address */
    if ((rc = ioctl(fd, SIOCGIFADDR, &ifr)) != 0)
    {
        close(fd);
        return;
    }

    char ipv4[16] = {0};
    addr = (struct sockaddr_in *)&ifr.ifr_addr;
    strncpy(ipv4, inet_ntoa(addr->sin_addr), sizeof(ipv4));

    /* 5. display */
    //    printf("%s:%s\n", name, inet_ntoa(addr->sin_addr));

    /* 6. close the socket */
    close(fd);

    sprintf(tmpstr, "%s:%s", name, inet_ntoa(addr->sin_addr));
    MyCharCopy(DisplayBuffer[line], tmpstr, sizeof(tmpstr));
}
void GetTemperature(int line)
{
    int fp;
    char tmpstr[30];
    int rawtemp;
    ClearBuffer(line);
    fp = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);
    read(fp, tmpstr, 5);
    close(fp);
    tmpstr[5] = 0;
    rawtemp = atoi(tmpstr);
    sprintf(tmpstr, "Temp:%d.%03d`C", rawtemp / 1000, rawtemp % 1000);
    MyCharCopy(DisplayBuffer[line], tmpstr, 21);
}
void GetLoadAverage(int line)
{
    FILE *fp;
    char tmpstr[30];
    char loadavg1[10], loadavg5[10], loadavg15[10];
    ClearBuffer(line);
    fp = fopen("/proc/loadavg", "rt");
    fscanf(fp, "%s %s %s", loadavg1, loadavg5, loadavg15);
    fclose(fp);
    sprintf(tmpstr, "Ld:%s %s %s", loadavg1, loadavg5, loadavg15);
    MyCharCopy(DisplayBuffer[line], tmpstr, 21);
}
void GetMeminfo(int line)
{
    FILE *fp;
    char tmpstr[30];
    int memtotal = -1, memfree = -1, swaptotal = -1, swapfree = -1;
    char fpstr[30];
    int fpint;
    ClearBuffer(line);
    fp = fopen("/proc/meminfo", "rt");
    while (1)
    {
        if (fscanf(fp, "%s %d kB", fpstr, &fpint) == -1)
            break;
        // printf("Data:%s %d\n",fpstr,fpint);
        if (memtotal == -1 && strcmp(fpstr, "MemTotal:") == 0)
            memtotal = fpint;
        if (memfree == -1 && strcmp(fpstr, "MemFree:") == 0)
            memfree = fpint;
        if (swaptotal == -1 && strcmp(fpstr, "SwapTotal:") == 0)
            swaptotal = fpint;
        if (swapfree == -1 && strcmp(fpstr, "SwapFree:") == 0)
            swapfree = fpint;
    }
    fclose(fp);
    if (swaptotal == 0)
        sprintf(tmpstr, "FreeMem:%d%%", 100 - memfree * 100 / memtotal);
    else
        sprintf(tmpstr, "Mem:%d%% Swap:%d%%", 100 - memfree * 100 / memtotal, 100 - swapfree * 100 / swaptotal);
    MyCharCopy(DisplayBuffer[line], tmpstr, 21);
}
void GetNetSpeed(int line)
{
    FILE *fp;
    char tmpstr[40];
    char fpstr[100], fpc;
    long long int value[16];
    int linecount = 0, i;
    static long long int lasttxb = 0, lastrxb = 0, lasttime = 0;
    long long int nowtxb = 0, nowrxb = 0, nowtime = 0;
    int txspeed, rxspeed;
    ClearBuffer(line);
    ClearBuffer(line + 1);
    fp = fopen("/proc/net/dev", "rt");
    while (linecount < 2)
    {
        fscanf(fp, "%c", &fpc);
        if (fpc == '\n')
            linecount++;
    }
    while (linecount < 3)
    {
        fscanf(fp, "%s", fpstr);
        for (i = 0; i < 16; i++)
            fscanf(fp, "%lld", &value[i]);
        if (strcmp(fpstr, "eth0:") == 0)
        {
            linecount++;
            nowrxb = value[0];
            nowtxb = value[8];
        }
    }
    fclose(fp);
    nowtime = time((time_t *)NULL);
    if (lasttime == 0)
    {
        sprintf(tmpstr, "RX:Gathering info");
        MyCharCopy(DisplayBuffer[line], tmpstr, 21);
        sprintf(tmpstr, "TX:Gathering info");
        MyCharCopy(DisplayBuffer[line + 1], tmpstr, 21);
    }
    else
    {
        txspeed = (int)(nowtxb - lasttxb) / 1024 / (nowtime - lasttime);
        rxspeed = (int)(nowrxb - lastrxb) / 1024 / (nowtime - lasttime);
        sprintf(tmpstr, "RX:%dKB/s", rxspeed);
        MyCharCopy(DisplayBuffer[line], tmpstr, 21);
        sprintf(tmpstr, "TX:%dKB/s", txspeed);
        MyCharCopy(DisplayBuffer[line + 1], tmpstr, 21);
    }
    lasttxb = nowtxb;
    lastrxb = nowrxb;
    lasttime = nowtime;
}
void GetSdcardUsage(int line)
{
    FILE *fp;
    char tmpstr[40];
    struct statfs diskInfo;
    ClearBuffer(line);
    statfs("/", &diskInfo);
    unsigned long long totalBlocks = diskInfo.f_bsize;
    unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;
    size_t mbTotalsize = totalSize >> 20;
    unsigned long long freeDisk = diskInfo.f_bfree * totalBlocks;
    size_t mbFreedisk = freeDisk >> 20;
    sprintf(tmpstr, "SD:%dM/%dM", mbFreedisk, mbTotalsize);
    MyCharCopy(DisplayBuffer[line], tmpstr, 21);
}
void GetSDA1SDB1() // Can be modified to other function
{
    char tmpstr[30];
    int sda1free, sdb1free;
    struct statfs diskInfo;
    ClearBuffer(7);
    statfs("/mnt/sda1", &diskInfo);
    unsigned long long totalBlocks = diskInfo.f_bsize;
    unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;
    size_t mbTotalsize = totalSize >> 20;
    unsigned long long freeDisk = diskInfo.f_bfree * totalBlocks;
    size_t mbFreedisk = freeDisk >> 20;
    sda1free = mbFreedisk * 100 / mbTotalsize;
    statfs("/mnt/sdb1", &diskInfo);
    totalBlocks = diskInfo.f_bsize;
    totalSize = totalBlocks * diskInfo.f_blocks;
    mbTotalsize = totalSize >> 20;
    freeDisk = diskInfo.f_bfree * totalBlocks;
    mbFreedisk = freeDisk >> 20;
    sdb1free = mbFreedisk * 100 / mbTotalsize;
    sprintf(tmpstr, "SDA1:%d%% SDB1:%d%%", sda1free, sdb1free);
    MyCharCopy(DisplayBuffer[7], tmpstr, 21);
}
void sig_term_handler(int signum, siginfo_t *info, void *ptr)
{
    char tmpstr[30];

    open("/home/pi/workspace/RaspberryPiOLEDMonitor/shutdownflag", O_RDWR | O_CREAT | O_TRUNC, 0666);

    ClearBuffer(-1); // line=-1 for all line
    UpdateBuffer();
    sprintf(tmpstr, "shutdowning!!!");
    MyCharCopy(DisplayBuffer[0], tmpstr, sizeof(tmpstr));
    UpdateBuffer();
}

void catch_sigterm()
{
    static struct sigaction _sigact;

    memset(&_sigact, 0, sizeof(_sigact));
    _sigact.sa_sigaction = sig_term_handler;
    _sigact.sa_flags = SA_SIGINFO;

    sigaction(SIGTERM, &_sigact, NULL);
}

int main()
{
    int count = 0;
    catch_sigterm();
    InitOled();
    ClearOled();
    ClearBuffer(-1);

    while (1)
    {
        if (access("/home/pi/workspace/RaspberryPiOLEDMonitor/shutdownflag", F_OK) != 0)
        {

            // ClearBuffer();
            GetSysTime();
            GetIP("eth0", 0);
            GetIP("wlan0", 1);
            GetTemperature(2);
            GetLoadAverage(3);
            GetMeminfo(4);
            GetNetSpeed(5);
            if (count % 5 == 0) // Decrease Fresh Frequency
            {
                GetSdcardUsage(7);
                //                GetSDA1SDB1();
            }
            UpdateBuffer();
            count++;
            //        printf("Fresh Count:%d\n",count);
            // sleep((hour>=23||hour<8)?60:2);
            sleep(2);
        }
    }
}
